﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using System.Data;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using Pcm.Retail.Client;
using System.Windows.Forms;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using CdcSoftware.Pivotal.Engine.UI.DataTypes.Navigation;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_Staff_Login : FormClientTask
    {
        /// <summary>
        /// On load of the form.
        /// </summary>
        /// <returns>Returns true for successful save else false</returns>
        /// <history>
        /// Revision#      Date            Author          Description
        /// 6.0.8.1        2011-05-09      DS              Initial version for Retail.                                                
        /// </history>
        public override void OnFormInitialized()
        {
            try
            {
                base.OnFormInitialized();
                
                bool masterPOS=false;

                ((PivotalButton)this.FormControl.GetControlByName("PivBtnClockIn")).Enabled = false;
                ((PivotalButton)this.FormControl.GetControlByName("PivBtnClockOut")).Enabled = false;

                //RA:16/Aug/2012:Change for Mobile System:Start:
                Id storePOSId = Id.Create(Globals.ServerSqlIndex("Employee", "RE_Store_POS_Id", this.SystemClient.UserProfile.EmployeeId));

                if (storePOSId != null)
                {
                    masterPOS = TypeConvert.ToBoolean(Globals.ServerSqlIndex("RE_Store_POS", "Master_POS", storePOSId));
                }
                if (masterPOS == false)
                {
                    PivotalMessageBox.Show("Staff Login/Logout can only be done on the Master POS. Please try on the Terminal designated as Master POS.",
                            MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    this.FormView.CloseForm(true);
                    return;
                }
                //RA:16/Aug/2012:Change for Mobile System:End:

                ButtonBarButton btnCancel = this.ButtonBar.Buttons["Cancel"];
                this.ButtonBar.Buttons.Clear();
                this.ButtonBar.Buttons.Add(btnCancel);

                //RA:16/Aug/2012:Change for Mobile System:Start:
                DataTable batch = Globals.GetDataTable("RE_In Progress Batch for Store for today?", new object[] { this.PrimaryDataRow["Store_Id"] }, "RE_Batch_Id");
                //RA:16/Aug/2012:Change for Mobile System:End:
                //Default the batch on the form.
                //DataTable batch = Globals.GetDataTable("RE_In Progress Batch for Store?", new object[] { this.PrimaryDataRow["Store_Id"] }, "RE_Batch_Id");
                if (batch != null && batch.Rows.Count == 1)
                    this.PrimaryDataRow["Login_Batch_Id"] = batch.Rows[0]["RE_Batch_Id"];

                //Event to always make the form non-dirty as we do not want to show the user prompt to save when existing the form.
                this.FormView.OnDirtyStateChanged += new EventHandler<Microsoft.Practices.CompositeUI.Utility.DataEventArgs<bool>>(FormView_OnDirtyStateChanged);

                //BR: Fix of QA Issue #2 - TODO: Currently since the navigation is from CommandHandlers, it is not being set
                //((PivotalTextBox)this.FormControl.GetControlByName("LoginStaffCode")).Focus();
                //Set the focus on Staff Code field.
                this.FormControl.GetControlByDataName("Login_Staff_Code").Focus();
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex, true);
            }
        }

        /// <summary>
        /// Event to make the form non-dirty as we do not want to show the user prompt to save when existing the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormView_OnDirtyStateChanged(object sender, Microsoft.Practices.CompositeUI.Utility.DataEventArgs<bool> e)
        {
            this.FormView.Dirty = false;
        } 
    }
}
